package top.infopub.job.sts.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.ProAcceptService;
import top.infopub.job.sts.dao.ProSelfCaeDao;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.mkt.ProAccept;
import top.infopub.model.sts.ProSelfCae;
import top.infopub.job.sts.service.ProSelfCaeService;
import top.infopub.job.util.ParserUtil;


@Service
public class ProSelfCaeServiceImpl extends GenericServiceImpl<ProSelfCae, String> implements ProSelfCaeService {

    @Autowired
    private ProSelfCaeDao proSelfCaeDao;

    @Autowired
    private ProAcceptService proAcceptService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Override
    public GenericDao<ProSelfCae, String> getDao() {
        return proSelfCaeDao;
    }

    @Override
    public List<ProSelfCae> selectProQaOfCae(Map<String, Object> where) {
        return proSelfCaeDao.selectProQaOfCae(where);
    }

    @Override
    public List<ProSelfCae> selectAreaQaOfCae(Map<String, Object> where) {
        return proSelfCaeDao.selectAreaQaOfCae(where);
    }

    @Override
    public List<ProSelfCae> selectAllProQaOfCae(Map<String, Object> where) {
        return proSelfCaeDao.selectAllProQaOfCae(where);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsProSelfCaeJob() {
        List<ProAccept> list = proAcceptService.selectNeedToSts();
        if (list == null || list.isEmpty()) {
            return;
        }
        for (ProAccept var : list) {
            ProSelfCae model = new ProSelfCae();
            OrgInfo where = new OrgInfo();
            where.setProjectCode(var.getProCode());
            List<OrgInfo> temp = orgInfoService.selectByEntityWhere(where);
            // 如果对应的项目公司没有创建  则此次不同步
            if (temp == null || temp.size() == 0) {
                continue;
            }
            else {
                model.setHrOrgAreaId(temp.get(0).getParentId());// 所属区域公司ID
                model.setHrOrgProId(temp.get(0).getId());// 项目公司ID
            }
            model.setSrcRefSguid(var.getId());
            model.setProCode(var.getProCode());
            model.setProName(var.getProName());
            model.setBelongsYear(ParserUtil.getYear(var.getNoticeDate()));
            model.setBelongsMonth(ParserUtil.getMonth(var.getNoticeDate()));
            model.setNoticeDate(var.getNoticeDate());
            model.setSubtotalCae(ParserUtil.tts2Unit(var.getSubtotalCae()));// 元
            model.setSubtotalCaeTts(var.getSubtotalCae());// 万元
            model.setSubtotalCaeHms(ParserUtil.tts2Hms(var.getSubtotalCae()));// 亿元
            model.setAddBy("system");
            model.setAddTime(new Date());
            model.setUptBy("system");
            model.setUptTime(new Date());
            this.insert(model);
        }
    }

}